package com.apexedu.biz.service.impl;

import com.apexedu.biz.action.PortalAction;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.identity.entity.TSysMenu;
import com.apexedu.identity.entity.TSysRole;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by wangjian on 14-4-9.
 * 添加到门户的资源查询
 */
@Service("tPortalService")
public class PortalServiceImpl extends AbstractBaseServiceSupport {
    @Override
    public void saveOrUpdate(Object o) throws Exception {
        PortalAction portalAction = (PortalAction) o;
        TSysMenu tSysMenu = portalAction.getTsysMenu();
        if (StringUtil.isNotBlank(tSysMenu.getMenuid())) {
            tSysMenu.setSftjdmh("1");
            baseDao.update(tSysMenu);
            queryRole(o,tSysMenu.getMenuid());
        }

    }

    @Override
    public void load(Object o) throws Exception {
        PortalAction portalAction = (PortalAction) o;
        if (null != portalAction) {
            String menuid = portalAction.getWid();//获取复选框中选中的值
            if (StringUtil.isNotBlank(menuid)) {
                TSysMenu tSysMenu = baseDao.findById(TSysMenu.class, menuid);//根据主键查询资源信息
                portalAction.setTsysMenu(tSysMenu);//将结果返回给页面
                queryRole(o,menuid);
            }

        }

    }

    @Override
    public List<TSysMenu> list(Object o, Pager pager) throws Exception {
        PortalAction portalAction = (PortalAction) o;
        StringBuilder hql = new StringBuilder("select t.menuid,t.menuname,t.menupath,(select g.appname from t_access_app_reg g where g.appid=t.belongtoapp )as ywxtmc,t.belongtoapp,t.state," +
                "(select c.zdmc from t_sys_code c where c.zdbm= t.srctype and c.zdlb='zylx')as srctype  from t_sys_menu t where t.sftjdmh = '1' ");//1：代表添加到门户
        List<Object> params = new LinkedList<Object>();
        TSysMenu menu = portalAction.getTsysMenu();//查询条件对象
        /********开始条件查询************/
        if (null != menu) {
            if (StringUtil.isNotBlank(menu.getMenuname())) {//资源名称
                hql.append(" and t.menuname like  ? ");
                params.add("%" + menu.getMenuname() + "%");
            }
            if (StringUtil.isNotBlank(menu.getState())) {//状态
                hql.append(" and t.state = ? ");
                params.add(menu.getState());
            }
            if (StringUtil.isNotBlank(menu.getBelongtoapp())) {//接入系统
                hql.append(" and t.belongtoapp = ? ");
                params.add(menu.getBelongtoapp());
            }
            portalAction.setTsysMenu(menu);
        }

        /********条件查询结束**********************/
        long c = DBUtil.count("select count(*) from (" + hql.toString() + ") c ", params.toArray());
        pager.setTotalRows(c);
        List<TSysMenu> list = DBUtil.queryPageBeanList(pager, hql.toString(), TSysMenu.class, params.toArray());
        return list;
    }

    @Override
    public boolean remove(Object o) throws Exception {
        PortalAction portalAction = (PortalAction) o;
        String wid = portalAction.getWid();
        boolean msg = false;
        if (StringUtil.isNotBlank(wid)) {
            String[] menu = wid.split(",");
            if (null != menu && menu.length > 0) {
                for (String id : menu) {
                    DBUtil.executeSQL("update t_sys_menu t set t.sftjdmh ='' where t.menuid= ?  ", id);
                }
            }
        }
        return msg;
    }

    public void queryRole(Object o,String menuid){
        PortalAction portalAction = (PortalAction) o;
        /*************将资源关联的角色，以及用户显示在资源详情页面中***************/
        String hql = "select t.roleid, t.rolename,t.memo, t.state, (select g.appname from t_access_app_reg g where g.appid= t.belongtoapp)as belongtoapp from t_sys_role t where " +
                "exists (select s.roleid from t_sys_role_menu s where s.roleid = t.roleid and s.menuid= ? )";
        List<TSysRole> list = DBUtil.queryAllBeanList(hql, TSysRole.class, menuid);
        /*************根据角色查询，角色下的所有用户***********************/
        if (null != list && list.size() > 0) {
            for (TSysRole tSysRole : list) {
                String hql_user="select * from t_sys_user t where exists(select s.userid from t_sys_user_role s where s.userid = t.userid and s.roleid = ? )" ;
                List<TSysUser> userList=DBUtil.queryAllBeanList(hql_user,TSysUser.class,tSysRole.getRoleid());
                if(null!=userList&&userList.size()>0){
                    tSysRole.setUserList(userList);
                }
            }
        }
        portalAction.setRoleList(list);

    }
}
